package com.ambow.servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ambow.dao.BookDao;

@WebServlet("/Book_Servlet")
public class Book_Servlet extends HttpServlet {


	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ud=request.getParameter("update");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");  
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession  hs = request.getSession(); 
		BookDao bookdao=new BookDao();
		if(ud.equals("book_modify")){
//			String position=new String(request.getParameter("textfield3").getBytes("ISO-8859-1"),"utf-8");
			String isbn = request.getParameter("isbn");
			String name = request.getParameter("name");
			String author = request.getParameter("author");
			String version = request.getParameter("version");
			String publish = request.getParameter("house");
			String date = request.getParameter("date");
			double price = Double.valueOf(request.getParameter("price"));

			String remark = request.getParameter("notes");


			System.out.println("新isbn："+isbn);
			System.out.println("新name："+name);
			System.out.println("新author："+author);
			System.out.println("新version："+version);
			System.out.println("新publish："+publish);
			System.out.println("新date："+date);
			System.out.println("新price："+price);
			System.out.println("新remark："+remark);
			int book_id=(Integer.parseInt( request.getParameter("id")));


			
			boolean f=bookdao.updateinfo(book_id,isbn,name, author,version,publish,date,price,remark);
			if(f==true){
//				response.sendRedirect(request.getContextPath()+"/Person_showServlet?val=personinfo");
				out.print("信息修改成功！下次登录查看。");
			}
			
		}
		else if(ud.equals("book_add")){
			
			String new_isbn = request.getParameter("ISBN");
			
			String new_name = request.getParameter("name");
//			byte [] b=new_name.getBytes("iso-8859-1");
//			new_name=new String(b,"utf-8");
			
			String new_author = request.getParameter("author");
			
			String new_edition = request.getParameter("editon");
			
			String new_house = request.getParameter("house");
//			byte [] b1=new_postion.getBytes("iso-8859-1");
//			new_postion=new String(b1,"utf-8");
			
			String new_date = request.getParameter("date");
			
			double new_price = Double.parseDouble((request.getParameter("price")));
//			byte [] b2=new_role.getBytes("iso-8859-1");
//			new_role=new String(b2,"utf-8");
			
			String new_remark = request.getParameter("textarea");
//			byte [] b3=new_remark.getBytes("iso-8859-1");
//			new_remark=new String(b3,"utf-8");
			
			System.out.println(new_isbn);
			System.out.println(new_name);
			System.out.println(new_author);
			System.out.println(new_edition);
			System.out.println(new_house);
			System.out.println(new_date);
			System.out.println(new_price);
			System.out.println(new_remark);
			
			boolean f=false;
			try {
				f = bookdao.addBook(new_isbn, new_name, new_author, new_edition,new_house, new_date, new_price, new_remark);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(f==true){
				out.print("添加成功！");
			}
			
		}
		else if(ud.equals("book_delete")){
			int id = Integer.parseInt((request.getParameter("id")));
			
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		    this.doGet(request, response);
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
